use tonic::{Request, Response, Status, transport::Server};

use system_all::system_service_server::{SystemService, SystemServiceServer};
use system_all::{
    LoginByAccountReq, LoginByAccountRes, LoginByMobileReq, LoginByMobileRes,
    GetUserListReq, GetUserListRes, CreateUserReq, CreateUserRes,
    GetRoleListReq, GetRoleListRes,
    GetMenuListReq, GetMenuListRes,
    GetDeptListReq, GetDeptListRes,
    GetOAuthUrlReq, GetOAuthUrlRes, OAuthLoginReq, OAuthLoginRes,
};

pub mod system_all {
    tonic::include_proto!("system.all");
}

#[derive(Default)]
pub struct MySystemService {}

#[tonic::async_trait]
impl SystemService for MySystemService {
    // 认证相关接口实现
    async fn login_by_account(
        &self,
        request: Request<LoginByAccountReq>,
    ) -> Result<Response<LoginByAccountRes>, Status> {
        println!("Got login_by_account request from {:?}", request.remote_addr());
        let req = request.into_inner();
        
        let reply = LoginByAccountRes {
            token: "mock_token_123".to_string(),
            message: format!("Hello {}! Login successful", req.name),
            code: 200,
        };
        Ok(Response::new(reply))
    }

    async fn login_by_mobile(
        &self,
        request: Request<LoginByMobileReq>,
    ) -> Result<Response<LoginByMobileRes>, Status> {
        println!("Got login_by_mobile request from {:?}", request.remote_addr());
        let req = request.into_inner();
        
        let reply = LoginByMobileRes {
            token: "mock_mobile_token_456".to_string(),
            message: format!("Mobile {} login successful", req.mobile),
            code: 200,
        };
        Ok(Response::new(reply))
    }

    // 用户管理接口实现
    async fn get_user_list(
        &self,
        request: Request<GetUserListReq>,
    ) -> Result<Response<GetUserListRes>, Status> {
        println!("Got get_user_list request from {:?}", request.remote_addr());
        
        let reply = GetUserListRes {
            users: vec![], // 这里应该是实际的用户列表
            total: 0,
            message: "Success".to_string(),
            code: 200,
        };
        Ok(Response::new(reply))
    }

    async fn create_user(
        &self,
        request: Request<CreateUserReq>,
    ) -> Result<Response<CreateUserRes>, Status> {
        println!("Got create_user request from {:?}", request.remote_addr());
        let req = request.into_inner();
        
        let reply = CreateUserRes {
            user_id: 12345, // 模拟创建的用户ID
            message: format!("User {} created successfully", req.username),
            code: 200,
        };
        Ok(Response::new(reply))
    }

    // 角色管理接口实现
    async fn get_role_list(
        &self,
        request: Request<GetRoleListReq>,
    ) -> Result<Response<GetRoleListRes>, Status> {
        println!("Got get_role_list request from {:?}", request.remote_addr());
        
        let reply = GetRoleListRes {
            roles: vec![], // 这里应该是实际的角色列表
            total: 0,
            message: "Success".to_string(),
            code: 200,
        };
        Ok(Response::new(reply))
    }

    // 菜单管理接口实现
    async fn get_menu_list(
        &self,
        request: Request<GetMenuListReq>,
    ) -> Result<Response<GetMenuListRes>, Status> {
        println!("Got get_menu_list request from {:?}", request.remote_addr());
        
        let reply = GetMenuListRes {
            menus: vec![], // 这里应该是实际的菜单列表
            message: "Success".to_string(),
            code: 200,
        };
        Ok(Response::new(reply))
    }

    // 部门管理接口实现
    async fn get_dept_list(
        &self,
        request: Request<GetDeptListReq>,
    ) -> Result<Response<GetDeptListRes>, Status> {
        println!("Got get_dept_list request from {:?}", request.remote_addr());
        
        let reply = GetDeptListRes {
            depts: vec![], // 这里应该是实际的部门列表
            message: "Success".to_string(),
            code: 200,
        };
        Ok(Response::new(reply))
    }

    // OAuth 接口实现
    async fn get_o_auth_url(
        &self,
        request: Request<GetOAuthUrlReq>,
    ) -> Result<Response<GetOAuthUrlRes>, Status> {
        println!("Got get_oauth_url request from {:?}", request.remote_addr());
        let req = request.into_inner();
        
        let reply = GetOAuthUrlRes {
            auth_url: format!("https://oauth.example.com/auth?provider={}", req.provider),
            state: "random_state_123".to_string(),
            message: "Success".to_string(),
            code: 200,
        };
        Ok(Response::new(reply))
    }

    async fn o_auth_login(
        &self,
        request: Request<OAuthLoginReq>,
    ) -> Result<Response<OAuthLoginRes>, Status> {
        println!("Got oauth_login request from {:?}", request.remote_addr());
        let req = request.into_inner();
        
        let reply = OAuthLoginRes {
            token: "oauth_token_789".to_string(),
            user_profile: None, // 这里应该是实际的用户资料
            message: format!("OAuth login with {} successful", req.provider),
            code: 200,
        };
        Ok(Response::new(reply))
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "[::1]:50051".parse().unwrap();
    let system_service = MySystemService::default();

    println!("SystemService listening on {}", addr);

    Server::builder()
        .add_service(SystemServiceServer::new(system_service))
        .serve(addr)
        .await?;

    Ok(())
}
